/**
 * AntDroid Copyright (c) 2008-2012 INRIA, University of Lille 1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Contact: nicolas.haderer@inria.fr
 *
 * Author: Haderer Nicolas
 */
package com.google.antranking.utils;


import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class AtUtils {


	/**
	 * Return true if phone has an 
	 * internet connection wifi/EDGE/GPRS/3G 
	 * @param mContext
	 * @return
	 */
	final static public Boolean hasinternetConnection(Context mContext){

		final ConnectivityManager conMgr =  (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (conMgr != null) {
			NetworkInfo i = conMgr.getActiveNetworkInfo();
			if (i != null) {
				if (!i.isConnected())
					return false;
				if (!i.isAvailable())
					return false;               
			}

			if (i == null)
				return false;

		} else
			return false;

		return true;
	}


	final static public void makeLongToast(Context context, String message){
		int duration = 2*1000;
		final Toast toast = Toast.makeText(context, message, duration);
		toast.show();
	}

	final static public void makeShortToast(Context context, String message){
		int duration = 5*1000;
		final Toast toast = Toast.makeText(context, message, duration);
		toast.show();
	}

	final static public  void showMessage(Context context, String message){

		final AlertDialog.Builder alert = new AlertDialog.Builder(context);
		alert.setMessage(message);
		alert.setCancelable(true);
		//alert.setIcon(R.drawable.home);
		alert.setPositiveButton("OK",null);
		alert.show();

	}

	final static public Location getLocation(Context ctx){

		final LocationManager locationManager = 
				(LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); 

		Location location = locationManager.
				getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if (location == null)
			location = locationManager.
			getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

		return location;
	}


	final static public void broadcastError(Context ctx, String title, String contentTitle, String message){

		final Intent intent = new Intent();
		intent.setAction(AtConstant.BROADCAST_LOG_ERROR);
		intent.putExtra("TITLE", title);
		intent.putExtra("CONTENT_TITLE", contentTitle);
		intent.putExtra("MESSAGE", message);
		ctx.sendBroadcast(intent);
	}

	final static public void broadcastLog(Context ctx, String title, String contentTitle, String message){

		final Intent intent = new Intent();
		intent.setAction(AtConstant.BROADCAST_LOG_ERROR);
		intent.putExtra("TITLE", title);
		intent.putExtra("CONTENT_TITLE", contentTitle);
		intent.putExtra("MESSAGE", message);
		ctx.sendBroadcast(intent);
	}

}
